/**
 * Created by Administrator on 2017/2/17 0017.
 */
(function(window){
    var arr=[],
        push=arr.push,
        slice=arr.slice;
    function Itcast(selector){
        return new Itcast.fn.init(selector)
    };
    Itcast.fn=Itcast.prototype={
        constructor:Itcast,
        length:0,
        init:function(selector){
                if(!selector)return this;
                arr.push.apply(this,Itcast.select(selector));

        }
    };
    Itcast.extend({
        select:function(selector){
            return document.querySelectorAll(selector);
        },
        isArrayLike:function(obj){
            if(Object.prototype.toString.call(obj)==="[ object Array ]"){
                return true;
            }
            var length="length" in obj&&obj.length;
            return typeof length=="number"&&length>=0;
        },
        each:function(arr,callback){
            if(Itcast.isArrayLike(arr)){
                for(var i=0;i<arr.length;i++){
                    if(callback.call(arr[i],i,arr[i])===false)break;
                }
            }else{
                for(var k in arr){
                    if(callback.call(arr[k],k,arr[k])===false)break;
                }
            }
        },
        map:function(arr,callback){
            var newArr=[],tmp;
            if(Itcast.isArrayLike(arr)){
                for(var i=0;i<arr.length;i++){
                    tmp=callback.call(arr[i],i);
                    if(tmp!=null){
                        newArr.push(tmp)
                    };
                };
            }else{
                for(var i=0;i<arr.length;i++){
                    tmp=callback.call(arr[i],i);
                    if(tmp!=null){
                        newArr.push(tmp);
                    };
                };
            };
            return newArr;
        },
    });
    Itcast.fn.extend({
        toArray:function(){
            return slice.call(this);
        },
        get:function(index){
            if(index===undefined){
                return this.toArray();
            }
            if(index<0){
                return this[this.length+index ];
            }else{
                return this[index];
            }
        },
        eq:function(i){
            var iobj=this.constructor();
            var dom=this.get(i);
            iobj[i]=dom;
            iobj.length=1;
            return iobj;
        },
        first: function ( ) {
            return this.eq(0);
        },
        last: function() {
            return this.eq( -1 );
        },
        each: function ( callback ) {
            return Itcast.each( this, callback );
        },
        map: function ( callback ) {
            return Itcast.map( this, callback );
        }
    })

})();